@file:Suppress("DEPRECATION")

package com.hope.onlinestudy.net

import com.google.gson.Gson
import com.hope.onlinestudy.BuildConfig
import me.jessyan.retrofiturlmanager.RetrofitUrlManager
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.*
import java.util.concurrent.TimeUnit


@Suppress("DEPRECATION", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
/**
 * @author hope.chen, QQ:77132995, email:kazeik@163.com
 * 2017 04 27 11:28
 * 类说明:
 */
class HttpNetUtils : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request().newBuilder()
            .build()
        return chain.proceed(request)
    }

    private val okClient: OkHttpClient by lazy {
        RetrofitUrlManager.getInstance().with(OkHttpClient.Builder())
            .addInterceptor(this)
            .retryOnConnectionFailure(true)
            .connectTimeout(time, TimeUnit.SECONDS)
            .readTimeout(time, TimeUnit.SECONDS)
            .writeTimeout(time, TimeUnit.SECONDS)
            .addInterceptor(
                HttpLoggingInterceptor().setLevel(
                    if (BuildConfig.LOG_DEBUG)
                        HttpLoggingInterceptor.Level.BODY
                    else HttpLoggingInterceptor.Level.NONE
                )
            )
            .build()
    }
    private val time: Long = 30
    private val retrofit: Retrofit? by lazy {
        Retrofit.Builder()
            .baseUrl("http://49.234.13.143/")
            .client(okClient)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }


    fun getParamsBody(dataMap: HashMap<String, Any>): RequestBody {
        val postInfoStr = Gson().toJson(dataMap)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr)
    }

    fun getParamsBody(data: ArrayList<Any>): RequestBody {
        val postInfoStr = Gson().toJson(data)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr)
    }

    fun getParamsStrBody(data: ArrayList<String>): RequestBody {
        val postInfoStr = Gson().toJson(data)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr)
    }

    fun getParamsStrBody(data: Any): RequestBody {
        val postInfoStr = Gson().toJson(data)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr)
    }

    fun getParamsBody(data: String): RequestBody {
        val postInfoStr = Gson().toJson(data)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr)
    }

    fun getParamsBody(data: ByteArray): RequestBody {
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
    }

    fun getManager(): ApiInter? {
        return retrofit?.create(ApiInter::class.java)
    }

    companion object {
        fun getInstance(): HttpNetUtils {
            synchronized(HttpNetUtils::class.java) {
                return HttpNetUtils()
            }
        }
    }
}
